package factorytest.iflytek.com.einkiflytekfactorytest.engineerui;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.core.app.ActivityCompat;
import factorytest.iflytek.com.einkiflytekfactorytest.R;
import factorytest.iflytek.com.einkiflytekfactorytest.ViewDialogFragment;


public class ScreenActivity extends Activity {

    private final static int MAX_COUNT = 6;
    private LinearLayout linearLayoutContainer;
    private int[] colorResIds;
    private int currentColorPos;

    private boolean setLongClick = false;

    private Context mContext;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        setContentView(R.layout.activity_screen);

        mContext = this;

        linearLayoutContainer = (LinearLayout) findViewById(R.id.linearLayoutContainer);

        colorResIds = getResources().getIntArray(R.array.color_gray_level);

        currentColorPos = 0;

        linearLayoutContainer.removeAllViews();
        linearLayoutContainer.setBackgroundColor(Color.WHITE);

        linearLayoutContainer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentColorPos ++;
                if (currentColorPos >= MAX_COUNT) {
                    currentColorPos = 0;

                    showDialog();
                    Toast.makeText(mContext, R.string.screen_test_toast_can_long_click, Toast.LENGTH_SHORT).show();

                    if (!setLongClick) {
                        setLongClick = true;
                        linearLayoutContainer.setOnLongClickListener(longClickListener);
                    }

                }

                if (currentColorPos == 0) {

                    linearLayoutContainer.removeAllViews();
                    linearLayoutContainer.setBackgroundColor(Color.WHITE);

                } else if (currentColorPos == 1) {

                    linearLayoutContainer.removeAllViews();
                    linearLayoutContainer.setBackgroundColor(Color.BLACK);

                } else {

                    final int pos = currentColorPos - 2;
                    setChildView((pos & 0x1) != 0, (pos & 0x2) != 0);

                }
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();

        Toast.makeText(mContext, R.string.screen_test_toast, Toast.LENGTH_SHORT).show();
    }

    private void setChildView(boolean colorRevert, boolean vertical) {

        linearLayoutContainer.removeAllViews();

        linearLayoutContainer.setOrientation(vertical ? LinearLayout.VERTICAL : LinearLayout.HORIZONTAL);

        for (int i = 0; i < colorResIds.length; i++) {
            View view = new View(mContext);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            lp.weight = 1;
            int color = colorResIds[i];
            if (colorRevert) {
                color = colorResIds[colorResIds.length - 1 - i];
            }
            view.setBackgroundColor(color);
            view.setClickable(false);
            linearLayoutContainer.addView(view, lp);
        }
    }

    private void showDialog() {

        showFinishDialog(R.string.screen_test, R.string.item_exit_test);

    }

    private void showFinishDialog(int titleRes, int msgRes) {
        final String title = getString(titleRes);
        final String msg = getString(msgRes);
        ViewDialogFragment dialog = ViewDialogFragment.newInstance(title, msg);

        dialog.show(getFragmentManager(),
                new ViewDialogFragment.Callback() {
                    @Override
                    public void onClick() {

                        Toast.makeText(mContext, R.string.test_ok, Toast.LENGTH_SHORT).show();
                        setResult(RESULT_OK);
                        ActivityCompat.finishAfterTransition(ScreenActivity.this);

                    }
                },
                new ViewDialogFragment.Callback() {
                    @Override
                    public void onClick() {

                        Toast.makeText(mContext, R.string.test_fail, Toast.LENGTH_SHORT).show();
                        setResult(RESULT_CANCELED);
                        ActivityCompat.finishAfterTransition(ScreenActivity.this);
                    }
                });
    }

    private View.OnLongClickListener longClickListener =
            new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    showDialog();
                    return true;
                }
            };




}
